package logic

import (
	"context"
	"shenzhou/C/srv/shenzhou"

	"shenzhou/C/api/internal/svc"
	"shenzhou/C/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userid := l.ctx.Value("userid").(int64)
	order, err := l.svcCtx.ShenZhou.CreateOrder(l.ctx, &shenzhou.CreateOrderRequest{
		UserId:  userid,
		GoodsId: int64(req.GoodsId),
		Amount:  req.Amount,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 200,
		Msg:  "订单创建成功",
		Data: map[string]interface{}{
			"order": order,
		},
	}, nil
}
